﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MetroManagement
{
    /// <summary>
    /// 线路应用服务类
    /// </summary>
    public class LineAppService : ApplicationService, ILineAppService
    {
        private readonly IRepository<Line, int> _lineRepository;
        private readonly IRepository<Station, int> _stationRepository;
        private readonly IRepository<LineStation> _lineStationRepository;

        public LineAppService(
            IRepository<Line, int> lineRepository,
            IRepository<Station, int> stationRepository,
            IRepository<LineStation> lineStationRepository)
        {
            _lineRepository = lineRepository;
            _stationRepository = stationRepository;
            _lineStationRepository = lineStationRepository;
        }

        // 线路站点信息
        public async Task<ResultResponse<LineStationDto>> LineAsync(string lineIds, string name)
        {
            // 查询站点信息
            var station = await _stationRepository
                .FirstAsync(m => m.Name == name);
            var stepInfo = ObjectMapper.Map<Station, SimpleStationDto>(station);
            // 查询线路信息
            var lineIdArray = lineIds.Split(",");
            var lineIdList = Array.ConvertAll<string, int>(lineIdArray, s => Convert.ToInt32(s));
            var lines = await _lineRepository
                .Where(m => lineIdList.Contains(m.Id))
                .ToListAsync();
            var lineDtos = ObjectMapper.Map<List<Line>, List<LineDto>>(lines);
            // 返回结果
            var dto = new LineStationDto()
            {
                StepInfo = stepInfo,
                LineList = lineDtos
            };
            return ResultResponse<LineStationDto>.Successful(dto);
        }

        // 列表
        public async Task<ResultResponse<IList<SimpleLineDto>>> ListAsync()
        {
            var lines = await _lineRepository
                .ToListAsync();
            var dtos = ObjectMapper.Map<List<Line>, List<SimpleLineDto>>(lines);
            return ResultResponse<IList<SimpleLineDto>>.Successful(dtos);
        }

        // 获取
        public async Task<ResultResponse<UniteLineDto>> GetAsync(int id)
        {
            // 线路信息
            var line = await _lineRepository
                .GetAsync(id);
            var dto = ObjectMapper.Map<Line, UniteLineDto>(line);
            // 站点信息
            var lineStationQuery = await _lineStationRepository
                .WithDetailsAsync(m => m.Line, m => m.Station);
            var lineStations = await lineStationQuery
                .Where(m => m.LineId == id)
                .ToListAsync();
            dto.StationsNumber = lineStations.Count;
            dto.First = lineStations
                .OrderBy(m => m.Seq).First().Station.Name;
            dto.End = lineStations
                .OrderBy(m => m.Seq).Last().Station.Name;
            // 运行站
            var runStation = lineStations
                .OrderBy(m => Guid.NewGuid())
                .First();
            dto.RunStationsName = runStation.Station.Name;
            dto.RemainingTime = (lineStations.Count - runStation.Seq) * 4;
            // 剩余站
            var stepList = lineStations
                .Where(m => m.Seq > runStation.Seq)
                .ToList();
            var stepDtos = ObjectMapper.Map<List<LineStation>, List<StationDto>>(stepList);
            dto.MetroStepList = stepDtos;
            // 返回结果
            return ResultResponse<UniteLineDto>.Successful(dto);
        }
    }
}